//
//  DettagliInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 22/05/13.
//
//

#import "DettagliInterventoViewController.h"
#import "DettaglioCD.h"
#import "OpenDettaglioInterventoViewController.h"
#import "Util.h"

@interface DettagliInterventoViewController ()

@end

@implementation DettagliInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated{
    self.array = [[self.intervento.dettagli allObjects] mutableCopy];
    [self.table reloadData];
}

-(IBAction) indietro{
	[[self navigationController] popViewControllerAnimated:YES];
}


// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Dettagli Nuovo Intervento";
	return [NSString stringWithFormat: @"Dettagli Intervento %@",self.intervento.numero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    DettaglioCD *di = (DettaglioCD *)[self.array objectAtIndex:indexPath.row];
    if([di.iddettaglio longLongValue]< 0){
        [cell setBackgroundColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1]];
        cell.textLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.detailTextLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.textLabel.text = [NSString stringWithFormat:@"N%d - %@",abs([di.iddettaglio intValue]),di.tipo];
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"%@",di.tipo];
    }
    cell.detailTextLabel.text = di.oggetto;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DettaglioCD *di = (DettaglioCD *)[self.array objectAtIndex:indexPath.row];
    NSLog(@"%@",di.iddettaglio);
    
    OpenDettaglioInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"OpenDettaglioInterventoViewController"];
    open.dett_intervento = di;
    open.intervento = self.intervento;
    open.nuovo = false;
    open.user = self.user;
    open.coreDataManager = self.coreDataManager;
    [self.navigationController pushViewController:open animated:TRUE];
}

-(IBAction) nuovo{
    @try {
        //Creiamo un'istanza di NSManagedObject per l'Entità che ci interessa
        DettaglioCD *di = [NSEntityDescription
                           insertNewObjectForEntityForName:@"DettaglioCD"
                           inManagedObjectContext: [self.coreDataManager managedObjectContext]];
        /*
        int cont = 0;
        for(DettaglioCD *dd in self.intervento.dettagli){
            if([dd.iddettaglio intValue] < 0){
                cont++;
            }
        }*/
        
        di.iddettaglio = [Util getNextDettaglioId];//[NSNumber numberWithInt:((cont * -1)-1)];
        [di addTecniciObject:[self.coreDataManager findUtente:self.user.idutente]];
        di.inizio = [Util roundDateToMinutes:[[NSDate alloc] init] andMinutes:15];
        
        
        OpenDettaglioInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"OpenDettaglioInterventoViewController"];
        open.dett_intervento = di;
        open.intervento = self.intervento;
        open.nuovo = true;
        open.user = self.user;
        open.coreDataManager = self.coreDataManager;
        [self.navigationController pushViewController:open animated:TRUE];
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
}


@end
